package com.itbaizhan.shopping_order_customer_api.controller;

import com.itbaizhan.shopping_common.pojo.Address;
import com.itbaizhan.shopping_common.pojo.Area;
import com.itbaizhan.shopping_common.pojo.City;
import com.itbaizhan.shopping_common.pojo.Province;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.AddressService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/address")
public class AddressController {
    @DubboReference
    private AddressService addressService;

    @GetMapping("/findAllProvince")
    public BaseResult<List<Province>> findAllProvince() {
        List<Province> allProvince = addressService.findAllProvince();
        return BaseResult.ok(allProvince);
    }

    @GetMapping("/findCityByProvince")
    public BaseResult<List<City>> findCityByProvince(Long provinceId) {
        List<City> allCity = addressService.findAllCity(provinceId);
        return BaseResult.ok(allCity);
    }

    @GetMapping("/findAreaByCity")
    public BaseResult<List<Area>> findAreaByCity(Long cityId) {
        List<Area> allArea = addressService.findAllArea(cityId);
        return BaseResult.ok(allArea);
    }

    @PostMapping("/add")
    public BaseResult add(@RequestHeader Long userId,@RequestBody Address address) {
        address.setUserId(userId);
        addressService.add(address);
        return BaseResult.ok();
    }

    @PutMapping("/update")
    public BaseResult update(@RequestBody Address address){
        addressService.update(address);
        return BaseResult.ok();
    }

    @GetMapping("/findById")
    public BaseResult<Address> findById(Long id){
        Address address = addressService.findById(id);
        return BaseResult.ok(address);
    }

    @DeleteMapping("/delete")
    public BaseResult delete(Long id){
        addressService.delete(id);
        return BaseResult.ok();
    }

    @GetMapping("/findByUser")
    public BaseResult<List<Address>> findByUser(@RequestHeader Long userId){
        List<Address> addressList = addressService.findByUserId(userId);
        return BaseResult.ok(addressList);
    }
}
